﻿
/**************************************************************************************************
* 对象： Sail.widget.ext.histogram
* 说明： 柱状图
*
**************************************************************************************************/
Sail.widget.ext.horizontalHsg = function(config){
    this.OCBC = null;
    
    Sail.apply(this, config);
    Sail.widget.ext.superclass.constructor.call(this);
}

Sail.extend(Sail.widget.ext.horizontalHsg,Sail.widget.ext,{
    load:function(){
        var config = {};
        
        //-----------------------------------------------------------
        //处理入参
        //-----------------------------------------------------------
        if(arguments.length!=0){
            var a = arguments[0];
            if(typeof a =="object"){
                Ext.apply(this,a);
                config = a;
            }else{
                this.url = a;
            }
        }
        
        if(this.url==undefined || this.url=="") {
            Ext.Msg.alert("信息：","没有找到关于数据源的定义！无法获取数据！");
            return;
        }

        //-----------------------------------------------------------
        this.clear();
        
        //-----------------------------------------------------------
        //-----------------------------------------------------------
        if(config.reader == undefined)      config.reader = this.reader;
        
        var reader = config.reader;
        if(reader==undefined){
            Ext.Msg.alert("信息：","没有找到关于数据解析对象的定义！无法解析数据！");
            return;
        }
        
        reader.yCoordinate =(reader.yCoordinate instanceof Array)?reader.yCoordinate:[reader.yCoordinate];
                
        var theObj = this;
        var _container = this.OCBC;
                
        var _callback = function(flag,getData){
            if(reader.type=="JsonReader"){
                
                //------------------------------------------------------------
                //处理表格显示的一些全局设置
                //------------------------------------------------------------
                var table_align    = (theObj.tabAlign!=undefined)?theObj.tabAlign:"center";
                var table_colWidth = (theObj.tabColWidth!=undefined)?theObj.tabColWidth:[50,100,360,100];
                var HSG_maxWidth   = table_colWidth[2]-10;
                var tableWidth     = eval(table_colWidth.join('+'))+2*4;

                //------------------------------------------------------------
                var _html = new Sail.stringBuffer();
                _html.append("<table align='"+table_align+"' border='0' cellpadding='0' cellspacing='2' width="+tableWidth+">");

                //------------------------------------------------------------
                var lineData = sc.getDataObj2(getData,reader.root);
                if(lineData==null ||lineData==undefined) return;
                
                var tmpYValue = [];
                for(var i=0;i<lineData.length;i++){
                    for(var j=0;j<reader.yCoordinate.length;j++){           //计算Y坐标轴最大值用临时数组
                        tmpYValue.push(lineData[i][reader.yCoordinate[j]]);
                    }
                }
                var maxValue = eval("Math.max("+tmpYValue.join(',')+")");

                //------------------------------------------------------------
                //------------------------------------------------------------
                for(var i=0;i<lineData.length;i++){
                    var _name = lineData[i][reader.xCoordinate];
                    _name = _name.replace('卷','<br>卷');

                    var _titles = [];
                    if(config.titleRender!=undefined && typeof config.titleRender == "function"){
                        for(var j=0;j<3;j++)
                        _titles.push(config.titleRender(lineData[i][reader.xCoordinate],lineData[i][reader.yCoordinate[j]],reader.yCoordinate[j],lineData[i]));
                    }
                    
                    //--------------------------------------------------------
                    _value = lineData[i][reader.yCoordinate[0]];
                    _width = parseInt((_value/maxValue)*HSG_maxWidth,10);
                    _html.append("<tr>");
                    _html.append("<td rowspan=3 width="+table_colWidth[0]+" class='hh_td1'>"+_name+"</td>");
                    _html.append("<td width="+table_colWidth[1]+" class='hh_td2'>本月计划量</td>");
                    _html.append("<td ><div class='hh_bgdiv_1' style='background-position-x:"+(_width-1000)+";'>");
                    
                    _text = (_titles.length==0)?_value:_titles[0];
                    _html.append("<div style='position:relative;left:"+(_width+5)+"'>"+_text+"</div>");
                    
                    _html.append("</div></td><td width=100>&nbsp;</td></tr>");

                    //--------------------------------------------------------
                    _value = lineData[i][reader.yCoordinate[1]];
                    _width = parseInt((_value/maxValue)*HSG_maxWidth,10);
                    _html.append("<tr><td class='hh_td2'>累计当日计划量</td>");
                    _html.append("<td ><div class='hh_bgdiv_2' style='background-position-x:"+(_width-1000)+";'>");

                    _text = (_titles.length==0)?_value:_titles[1];
                    _html.append("<div style='position:relative;left:"+(_width+5)+"'>"+_text+"</div>");
                    _html.append("</div></td></tr>");

                    //--------------------------------------------------------
                    _value = lineData[i][reader.yCoordinate[2]];
                    _width = parseInt((_value/maxValue)*HSG_maxWidth,10);
                    _html.append("<tr><td class='hh_td2'>累计当日产量</td>");
                    _html.append("<td ><div class='hh_bgdiv_3' style='background-position-x:"+(_width-1000)+";'>");

                    _text = (_titles.length==0)?_value:_titles[2];
                    _html.append("<div style='position:relative;left:"+(_width+5)+"'>"+_text+"</div>");
                    _html.append("</div></td></tr>");
                    _html.append("<tr height=6><td></td></tr>");

                }
                
                _container.innerHTML = _html.toString();
                //------------------------------------------------------------
                //------------------------------------------------------------
                //debugger;
                
                
            }

        };
        
        sc.postData(this.url,{},_callback);
        
                
    },
    
    clear : function(){
    },
    
    createWidgetElement:function(){
        var containerDiv = this.getOCD();
        
        //-----------------------------------------------------------
        //-----------------------------------------------------------
        if(this.showTitle===true){
            var tbarDiv = document.createElement( "DIV" );     //创建标题容器
            containerDiv.appendChild( tbarDiv );               //添加到包裹容器
            Sail.apply(tbarDiv.style,{border:"0 #ababab solid",left:0,top:0,height:21,cursor:"default",position:"absolute"});
            
            tbarDiv.innerHTML = "<table style='cursor:hand' height=21 cellspacing=0>"
                +"<tr height=21><td class='tbar_lcorner' width=2></td><td width=4 class='tbar_back'></td>"
                +"<td class='tbar_app'  width=16></td><td width=4 class='tbar_back'></td>"
                +"<td height=21 class='tbar_back' style='padding-top:3'>"+this.title+"</td>"
                +"<td width=2 class='tbar_back'></td><td class='tbar_right' width=22></td></tr></table>";
            this.barDiv = tbarDiv;
        }

        var formDiv = document.createElement( "DIV" );     //创建标题容器
        containerDiv.appendChild( formDiv );               //添加到包裹容器

        //debugger;
        var formTop  = (this.showTitle===true)?21:0;
        var formBord = (this.border!=undefined)?this.border:"1 #99BBE8 solid";
        var formConfig = {
            left:0,
            top:formTop,
            width : this.width,
            height:this.height-21,
            overflow : "auto",
            cursor:"default",
            position:"absolute"
        };
        if(this.showBorder===true) formConfig.border = formBord;
        Sail.apply(formDiv.style,formConfig);

        //-----------------------------------------------------------
        //-----------------------------------------------------------
        this.OCBC   = formDiv;
        
    }
});




/**************************************************************************************************
* 对象： Sail.widget.ext.customGrid
* 说明： 柱状图
*
**************************************************************************************************/
Sail.widget.ext.customGrid = function(config){
    Sail.apply(this, config);
    Sail.widget.ext.superclass.constructor.call(this);
}

Sail.extend(Sail.widget.ext.customGrid,Sail.widget.ext,{
    insert : function(html){
        this.body.innerHTML = html;
    },
    createWidgetElement:function(){
        var containerDiv = this.OCD;
        containerDiv.style.border = "1px #99BBE8 solid";
        
        //-----------------------------------------------------------
        //标题
        //-----------------------------------------------------------
        var tIconDiv = document.createElement("<div class='cg_icon' style='left:7;top:5;'></div>");
        tIconDiv.innerText = this.title;
        tIconDiv.id = this.id+"_title";
        
        //-----------------------------------------------------------
        //body
        //-----------------------------------------------------------
        var bodyHeight = this.height-25-2;
        var tBodyDiv = document.createElement("DIV");
        tBodyDiv.style.border   = '0px solid #ff0000';
        tBodyDiv.style.height   = bodyHeight;
        tBodyDiv.style.width    ='100%';
        tBodyDiv.style.overflow = "auto";
        tBodyDiv.id = this.id+"_body";

        //-----------------------------------------------------------
        //标题BAR
        //-----------------------------------------------------------
        var tbarDiv = document.createElement("DIV");     
        tbarDiv.className='cg_titleBar';
        tbarDiv.id = this.id+"_tbar";
        
        //-----------------------------------------------------------
        //添加
        //-----------------------------------------------------------
        tbarDiv.appendChild(tIconDiv);
        containerDiv.appendChild( tbarDiv );
        containerDiv.appendChild(tBodyDiv);

        //-----------------------------------------------------------
        //-----------------------------------------------------------
        this.titleDiv = tIconDiv;
        this.body = tBodyDiv
        this.titleBar = tbarDiv;
    }
});